<?php

require_once dirname(__FILE__) . '/../lib/examenGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/examenGeneratorHelper.class.php';

/**
 * examen actions.
 *
 * @package    sistema_itec_new
 * @subpackage examen
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class examenActions extends autoExamenActions {
    
    protected function buildQuery() {
        $query = parent::buildQuery();
        $query->where($query->getRootAlias() . '.fecha >= ?', date('Y-m-d', strtotime(str_replace('/', '-', sfConfig::get('app_inscripcion_examenes_desde')))));
        return $query;
    }

    function executeAjaxNew(sfWebRequest $request) {
        $form = new examenForm();
        $module = $this->getModuleName();

        if ($request->isMethod(sfRequest::POST)) {
            $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
            if ($form->isValid()) {
                $object = $form->save();

                return $this->renderText('SUCCESS. [id:' . $object->getid() . '][nombre:' . $object . '][model:' . $module . ']');
            }
        }

        $title = 'Nuevo ' . ucwords(str_replace('_', ' ', $module));
        $backend_route = sfConfig::get('app_backend_route');
        $html = <<<EOF
            <form method="post" action="/$backend_route/$module/AjaxNew">
                <h2>$title</h2>
                <div class="box">
                    $form
                </div>
EOF;
        return $this->renderText($html);
    }

    public function executeCreate(sfWebRequest $request) {
        $this->form = $this->configuration->getForm();
        $this->examen = $this->form->getObject();

        $examen = $request->getParameter('examen');
        $examen['fecha'] = date('Y-m-d', strtotime(str_replace('/', '-', $examen['fecha'])));
        $request->setParameter('examen', $examen);

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->examen = $this->getRoute()->getObject();
        $this->form = $this->configuration->getForm($this->examen);

        $examen = $request->getParameter('examen');
        $examen['fecha'] = date('Y-m-d', strtotime(str_replace('/', '-', $examen['fecha'])));
        $request->setParameter('examen', $examen);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executePrintList(sfWebRequest $request) {
        $examen = Doctrine::getTable('examen')->createQuery('e')->innerJoin('e.Modulo m')->where('e.id = ?', $request->getParameter('id'))->execute();
        $this->examen = $examen[0];
        $this->inscriptos = Doctrine::getTable('examenAlumno')->getInscriptos($request->getParameter('id'));
        $this->setLayout(false);
        sfConfig::set('sf_web_debug', false);
        $filename = $this->examen->getModulo();
        header("Content-Type: application/vnd.ms-excel ; charset=UTF-8");
        header('Content-Disposition: attachment; filename="'.$filename.'.xls"');
        return $this->renderPartial('printList');
    }
    
    public function executeVerRecaudacion(sfWebRequest $request) {
        $this->fecha = sfConfig::get('app_inscripcion_examenes_desde');
        $result = Doctrine::getTable('examenAlumno')
                ->createQuery('ea')
                ->select('count(ea.id) AS cantidad')
                ->innerJoin('ea.Examen e')
                ->where('ea.pago = ?', 1)
                ->andWhere('e.fecha > STR_TO_DATE(?, "%d/%m/%Y")', $this->fecha)
                ->fetchArray();
        sfConfig::set('sf_web_debug', false);
        $this->setLayout(false);
        return $this->renderText('La recaudación total desde el '.$this->fecha.' es de $'.$result[0]['cantidad'] * 10);
    }

}
